/*
Copyright 2022 The KubeOne Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
	"bytes"
	"flag"
	"fmt"
	"io"
	"log"
	"os"
	"text/template"

	"github.com/MakeNowJust/heredoc/v2"

	"k8c.io/kubeone/test/e2e"

	"sigs.k8s.io/yaml"
)

type Infrastructure struct {
	Name         string `json:"name"`
	AlwaysRun    bool   `json:"alwaysRun"`
	RunIfChanged string `json:"runIfChanged"`
	Optional     bool   `json:"optional"`
}

type KubeoneTest struct {
	Scenario           string           `json:"scenario"`
	InitVersion        string           `json:"initVersion"`
	UpgradedVersion    string           `json:"upgradedVersion"`
	InitKubeOneVersion string           `json:"initKubeOneVersion"`
	Infrastructures    []Infrastructure `json:"infrastructures"`
}

var (
	// General generator flags
	filePathFlag    string
	packageNameFlag string
	outputFileFlag  string
	outputType      string

	// GitHub generator flags
	createGitHubIssues bool
	providerName       string
	scenarioName       string
	kubeoneVersion     string
)

const fileHeader = `// Code generated by e2e/generator, DO NOT EDIT.
//go:build e2e

package {{.PackageName}}

import (
	"testing"
)

func TestStub(t *testing.T) {
	t.Skip("stub is skipped")
}`

func main() {
	// General generator flags
	flag.StringVar(&filePathFlag, "file", "", "path to the YAML file with tests definitions to generate")
	flag.StringVar(&packageNameFlag, "package", "e2e", "the name of the generated Go package")
	flag.StringVar(&outputType, "type", "", "the type of the generator output (yaml|go|gh)")
	flag.StringVar(&outputFileFlag, "output", "-", "the name of the file to write to, - for stdout")

	// GitHub generator flags
	flag.BoolVar(&createGitHubIssues, "create-issues", false, "create issues automatically in the kubeone repository")
	flag.StringVar(&providerName, "provider", "", "the name of the provider to generate GitHub ticket for (empty generates for all)")
	flag.StringVar(&scenarioName, "scenario", "", "the name of the scenario to generate GitHub ticket for (empty generates for all)")
	flag.StringVar(&kubeoneVersion, "k1version", "", "kubeone version to be used for generating GitHub tickets")
	flag.Parse()

	if filePathFlag == "" {
		log.Fatal("-file argument in required")
	}

	var generatorType e2e.GeneratorType

	switch outputType {
	case "":
		log.Fatal("-type argument is required")
	case "go":
		generatorType = e2e.GeneratorTypeGo
	case "yaml":
		generatorType = e2e.GeneratorTypeYAML
	case "gh":
		generatorType = e2e.GeneratorTypeGitHub
	default:
		log.Fatalf("-type=%s argument is invalid", outputType)
	}

	var outputBuf io.ReadWriter = &bytes.Buffer{}

	if outputFileFlag == "-" {
		outputBuf = os.Stdout
	}

	buf, err := os.ReadFile(filePathFlag)
	if err != nil {
		log.Fatal(err)
	}

	var getTests []KubeoneTest

	if err = yaml.UnmarshalStrict(buf, &getTests); err != nil {
		log.Fatal(err)
	}

	switch generatorType {
	case e2e.GeneratorTypeGo:
		err = template.Must(template.New("").Parse(fileHeader)).Execute(outputBuf, struct {
			PackageName string
		}{
			PackageName: packageNameFlag,
		})
		if err != nil {
			log.Fatal(err)
		}
	case e2e.GeneratorTypeYAML:
		fmt.Fprint(outputBuf, heredoc.Doc(`
		# Code generated by e2e/generator, DO NOT EDIT.
		presubmits:
		`))
	case e2e.GeneratorTypeGitHub:
		err = generateGitHubIssues(outputBuf, getTests, providerName, scenarioName, kubeoneVersion, createGitHubIssues)
		if err != nil {
			log.Fatal(err)
		}

		return
	}

	for _, genTest := range getTests {
		scenario, ok := e2e.Scenarios[genTest.Scenario]
		if !ok {
			log.Fatalf("%q scenario is not defined", genTest.Scenario)
		}

		for _, genInfra := range genTest.Infrastructures {
			infra, ok := e2e.Infrastructures[genInfra.Name]
			if !ok {
				log.Fatalf("%q infra is not defined", genInfra.Name)
			}

			scenario.SetInfra(infra)
			versions := []string{genTest.InitVersion}
			if genTest.UpgradedVersion != "" {
				versions = append(versions, genTest.UpgradedVersion)
			}
			scenario.SetVersions(versions...)

			if scn, ok := scenario.(e2e.ScenarioStable); ok {
				scn.SetInitKubeOneVersion(genTest.InitKubeOneVersion)
			}

			cfg := e2e.ProwConfig{
				AlwaysRun:    genInfra.AlwaysRun,
				RunIfChanged: genInfra.RunIfChanged,
				Optional:     genInfra.Optional,
			}

			if err = scenario.GenerateTests(outputBuf, generatorType, cfg); err != nil {
				log.Fatal(err)
			}
		}
	}

	if outputFileFlag != "-" {
		data, _ := io.ReadAll(outputBuf)
		if err = os.WriteFile(outputFileFlag, data, 0644); err != nil { //nolint:gosec
			log.Fatalf("failed to write file %v", err)
		}
	}
}
